from __future__ import annotations

from dataclasses import dataclass
from typing import Optional, Dict, Any

from gssgca.core.standard_loader import provider


@dataclass
class CraneChoice:
    model: str
    capacity_t: float
    radius_m: float
    boom_length_m: float
    note: str


def select_crane(hook_load_kN: float, working_radius_m: float, hook_height_m: float, catalog_key: str = "truck_cranes") -> CraneChoice:
    """吊车选型（简化）：从型录中找满足幅度与额定起重量的机型。"""

    load_t = hook_load_kN / 9.81
    catalog = provider._load_json("crane_catalog.json").get(catalog_key, [])
    chosen: Optional[Dict[str, Any]] = None
    for it in catalog:
        if working_radius_m <= it.get("max_radius_m", 0.0) and hook_height_m <= it.get("max_hook_height_m", 0.0) and load_t <= it.get("capacity_t", 0.0):
            chosen = it
            break
    if not chosen and catalog:
        chosen = max(catalog, key=lambda x: x.get("capacity_t", 0.0))
        note = "取最大吨位但可能不满足工况，需复核曲线"
    elif chosen:
        note = "满足基本幅度与高度要求，按起重性能曲线复核"
    else:
        return CraneChoice("N/A", 0.0, working_radius_m, hook_height_m, "无型录")
    return CraneChoice(model=str(chosen.get("model")), capacity_t=float(chosen.get("capacity_t", 0.0)), radius_m=working_radius_m, boom_length_m=float(chosen.get("max_hook_height_m", 0.0)), note=note)


